package com.artisan.service.wx.impl;

import com.artisan.dao.CustomerCodeMapper;
import com.artisan.dao.CustomerMapper;
import com.artisan.medium.wx.service.WxUserService;
import com.artisan.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;

/**
 * Created by yang on 2017/6/18.
 */
@Service("wxUserService")
public class WxUserServiceImpl implements WxUserService{
    @Autowired
    private CustomerCodeMapper customerCodeMapper;

    @Autowired
    private CustomerMapper customerMapper;
    @Override
    public int checkSendOK(String phone) {
        CustomerCodeExample customerCodeExample = new CustomerCodeExample();
        CustomerCodeExample.Criteria bCriteria = customerCodeExample.createCriteria();
        bCriteria.andSendModeEqualTo((short)2);
        bCriteria.andSendTargetEqualTo(phone);
        bCriteria.andStateEqualTo(1);
        bCriteria.andTypeEqualTo(2);
        Calendar nowTime = Calendar.getInstance();
        nowTime.add(Calendar.MINUTE, -1);//30分钟前的时间
        bCriteria.andSendTimeGreaterThanOrEqualTo(nowTime.getTime());
        return customerCodeMapper.countByExample(customerCodeExample);
    }

    @Override
    public void insertSmsInfo(String phone, String sms) {
        Calendar nowTime = Calendar.getInstance();
        CustomerCode code=new CustomerCode();
        code.setPurpose((short)1);
        code.setSendMode((short)2);
        code.setSendTarget(phone);
        code.setState(1);
        code.setType(2);
        code.setSendTime(nowTime.getTime());
        code.setValue(sms);
        customerCodeMapper.insert(code);
    }

    @Override
    public Customer getCustomerByOpenId(String opendid) {
        CustomerExample customerExample = new CustomerExample();
        CustomerExample.Criteria bCriteria = customerExample.createCriteria();
        bCriteria.andOpenIdEqualTo(opendid);
        List<Customer>customers= customerMapper.selectByExample(customerExample);
        if(customers.size()!=0){
            return customers.get(0);
        }
        return null;
    }

    @Override
    public Customer getCustomerByPhone(String phone) {
        CustomerExample customerExample = new CustomerExample();
        CustomerExample.Criteria bCriteria = customerExample.createCriteria();
        bCriteria.andPhoneEqualTo(phone);
        List<Customer>customers= customerMapper.selectByExample(customerExample);
        if(customers.size()!=0){
            return customers.get(0);
        }
        return null;
    }

    @Override
    public int checkLogin(String phone, String value) {
        CustomerCodeExample customerCodeExample=new CustomerCodeExample();
        CustomerCodeExample.Criteria cCriteria = customerCodeExample.createCriteria();
        cCriteria.andSendTargetEqualTo(phone);
        cCriteria.andValueEqualTo(value);
        cCriteria.andSendModeEqualTo((short)2);
        cCriteria.andStateEqualTo(1);
        Calendar nowTime = Calendar.getInstance();
        nowTime.add(Calendar.MINUTE, -5);//30分钟前的时间
        cCriteria.andSendTimeGreaterThanOrEqualTo(nowTime.getTime());
        return customerCodeMapper.countByExample(customerCodeExample);
    }

    @Override
    public int bingDingPhone(Customer customer) {
         customerMapper.insertSelective(customer);
        return customer.getId();
    }

    @Override
    public void loginOut(Customer customer) {
        Customer cust=  customerMapper.selectByPrimaryKey(customer.getId());
        cust.setOpenId(null);
        customerMapper.updateByPrimaryKey(cust);
    }

    @Override
    public void login(Customer customer) {
        customerMapper.updateByPrimaryKey(customer);
    }


}
